package com.xiao.mall.message;

import com.xiao.mall.foundation.config.DefaultProfileUtil;
import com.xiao.mall.foundation.config.PlatformConstants;
import com.xiao.mall.foundation.utils.AppLogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collection;

@EnableConfigurationProperties({LiquibaseProperties.class})
@Slf4j
@SpringBootApplication(scanBasePackages = {"com.xiao.mall.foundation.*", "com.xiao.mall.message.*"})
public class MessageApplication {

    private final Environment env;
    public MessageApplication(Environment env) {
        this.env = env;
    }

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MessageApplication.class);
        DefaultProfileUtil.addDefaultProfile(app);
        Environment env = app.run(args).getEnvironment();
        AppLogUtil.logApplicationStartup(log, env);
    }

    @PostConstruct
    public void initApplication() {
        Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
        if (activeProfiles.contains(PlatformConstants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(PlatformConstants.SPRING_PROFILE_PRODUCTION)) {
            log.error("You have misconfigured your application! It should not run " +
                    "with both the 'dev' and 'prod' profiles at the same time.");
        }
        if (activeProfiles.contains(PlatformConstants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(PlatformConstants.SPRING_PROFILE_CLOUD)) {
            log.error("You have misconfigured your application! It should not " +
                    "run with both the 'dev' and 'cloud' profiles at the same time.");
        }
    }
}
